package lumis.portal.sendmail.dao.hibernate;

import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;

import lumis.portal.sendmail.IMailAttachment;

import org.hibernate.lob.BlobImpl;

/**
 * Implementation of {@link IMailAttachment} used by {@link SendMailDaoHib}.
 * 
 * @version $Revision: 8289 $ $Date: 2007-09-24 17:46:32 -0300 (Mon, 24 Sep 2007) $
 * @since 4.0.4
 */
class MailAttachment implements IMailAttachment
{
	private String id;
	private Blob content;
	private String name;
	private String description;
	private String contentType;
	private MailBody mailBody;

	public String getContentType()
	{
		return contentType;
	}

	public void setContentType(String contentType)
	{
		this.contentType = contentType;
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public Blob getContent()
	{
		return content;
	}

	public void setContent(Blob content)
	{
		this.content = content;
	}

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public InputStream getContentInputStream()
	{
		try
		{
			if (content != null)
				return content.getBinaryStream();
			else
				return null;
		}
		catch (SQLException e)
		{
			throw new IllegalStateException(e);
		}
	}

	public void setContent(InputStream inputStream, int length)
	{
		content = new BlobImpl(inputStream, length);
	}

	/**
	 * Returns the mail body this attachment belongs to.
	 * @return the mail body this attachment belongs to.
	 * @since 4.0.11
	 */
	public MailBody getMailBody()
	{
		return mailBody;
	}

	/**
	 * Sets the mail body for this attachment.
	 * @param mailBody the mail body to set.
	 * @since 4.0.11
	 */
	public void setMailBody(MailBody mailBody)
	{
		this.mailBody = mailBody;
	}
}
